package com.garen.fire.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.garen.common.MsgBean;
import com.garen.common.PageBean;
import com.garen.fire.biz.IFireCaseRecordBiz;
import com.garen.fire.dao.IFireCarDao;
import com.garen.fire.dao.IFireCaseRecordDao;
import com.garen.fire.entity.FireCar;
import com.garen.fire.entity.FireCaseRecord;
import com.garen.fire.entity.FireDuty;
import com.garen.fire.web.FireCaseRecordAction;
import com.garen.sys.entity.FireUser;


@Service
public class FireCaseRecordBizImpl implements IFireCaseRecordBiz {

	protected static Log log = LogFactory.getLog(FireCaseRecordBizImpl.class);

	@Autowired
	private IFireCaseRecordDao fireCaseRecordDao;

	@Autowired
	private IFireCarDao fireCarDao;
	
	@Override
	public JSONObject getFireCaseRecordList(MsgBean mbean, String ajbh, String orgId) {
		JSONObject resultJson = new JSONObject();
		List<FireCaseRecord> recordList = fireCaseRecordDao.getFireCaseRecordList(ajbh, orgId);
		JSONArray userList = new JSONArray();
		// 过滤,获取待命消防员
		for (FireCaseRecord record : recordList) {
			if (record.getRecordId() == null) {
				userList.add(createUserByRecord(record));
			}
		}
		if (recordList.size() == userList.size()) {
			log.debug("没有处警信息");
			return resultJson;
		}
		resultJson.put("userList", userList);
		// 获取所有车辆
		List<FireCar> sendList = fireCarDao.getFireCarList(orgId);
		// 关联车和人
		for (FireCar car : sendList) {
			JSONArray menList = new JSONArray();
			for (FireCaseRecord record : recordList) {
				if (car.getCarId().equals(record.getFireCar())) {
					menList.add(createUserByRecord(record));
				}
			}
			car.setMenList(menList);
		}
		//待命车辆
		List<FireCar> carList = new ArrayList<>();
		Iterator<FireCar> it = sendList.iterator();
		while(it.hasNext()){
			FireCar car = it.next();
			if(car.getMenList().isEmpty()){
				log.debug("待命车辆:" + car.getCarId() +","+ car.getCarNum());
				it.remove();
				carList.add(car);
			}
		}
		resultJson.put("sendList", sendList);
		resultJson.put("carList", carList);
		return resultJson;
	}

	// 根据排班记录生成用户
	private JSONObject createUserByRecord(FireCaseRecord record) {
		JSONObject user = new JSONObject();
		user.put("fireMan", record.getFireMan());
		user.put("fireManName", record.getFireManName());
		user.put("userPoliceRank", record.getFirePoliceRank());
		user.put("fireAge", record.getFireManAge());
		user.put("recordId", record.getRecordId());
		return user;
	}
		
	public JSONObject getFireCaseRecordList2(MsgBean mbean,String ajbh,String orgId){
		if(StringUtils.isEmpty(ajbh)) return null;
		List<FireCaseRecord> recordList = fireCaseRecordDao.getFireCaseRecordList(ajbh,orgId);
		if(recordList.isEmpty()) return null;
		String orgName1 = null, carName1 = null, manName1 = null;
		JSONArray jarr1 = null, jarr2 = null, jarr3 = null;
		jarr1 = new JSONArray();
		for (FireCaseRecord record : recordList) {
			String orgName2 = record.getFireOrgName();
			String carName2 = record.getFireCarName();
			String manName2 = record.getFireManName();
			if (orgName2.equals(orgName1) == false) {
				JSONObject json = new JSONObject();
				json.put("orgId", record.getFireOrg());
				json.put("orgName", record.getFireOrgName());
				json.put("fireGpsLat", record.getFireGpsLat());
				json.put("fireGpsLng", record.getFireGpsLng());
				
				jarr1.add(json);
				jarr2 = new JSONArray();
				json.put("list", jarr2);
			}
			if (carName2.equals(carName1) == false) {
				JSONObject json = new JSONObject();
				json.put("carId", record.getFireCar());
				json.put("carUUID", record.getFireCarUUID());
				json.put("cphm", record.getFireCarName());
				jarr2.add(json);
				jarr3 = new JSONArray();
				json.put("menList", jarr3);
			}
			if (manName2.equals(manName1) == false) {
				JSONObject json = new JSONObject();
				json.put("fierId", record.getFireMan());
				json.put("name", record.getFireManName());
				jarr3.add(json);
			}
			orgName1 = orgName2;
			carName1 = carName2;
			manName1 = manName2;
		}
		JSONObject resultJson = jarr1.size() > 0? jarr1.getJSONObject(0):null;
		return resultJson;
	}
	
	//批量保存
	@Override
	public void saveFireCaseRecordAll(MsgBean mbean, String ajbh,Integer orgId,List<FireCaseRecord> recordList){
		//删除旧的
		FireCaseRecord caseRecord = new FireCaseRecord();
		caseRecord.setFireAjbh(ajbh);
		caseRecord.setFireOrg(orgId);
		fireCaseRecordDao.del(caseRecord);
		for(FireCaseRecord record : recordList){
			record.setFireDate(new Date());
			//record.setFireOrg(orgId);
			fireCaseRecordDao.add(record);
		}
	}
		
	/*
	*分页查询
	*/
	@Override
	public List<FireCaseRecord> getFireCaseRecordList(PageBean<FireCaseRecord> pb,FireCaseRecord fireCaseRecord){
		return fireCaseRecordDao.getPageList(pb,fireCaseRecord);
	}

	/*
	*分页查询
	*/
	@Override
	public List<FireCaseRecord> getFireCaseRecordList(FireCaseRecord fireCaseRecord){
		return fireCaseRecordDao.getList(fireCaseRecord);
	}

	/*
	*查询个体
	*/
	@Override
	public FireCaseRecord getFireCaseRecord(FireCaseRecord fireCaseRecord){
		return fireCaseRecordDao.get(fireCaseRecord);
	}

	/*
	*保存
	*/
	@Override
	public int saveFireCaseRecord(MsgBean mbean,FireCaseRecord fireCaseRecord){
		int ret = -1;
		if(fireCaseRecord.getRecordId() == null){
			ret = fireCaseRecordDao.add(fireCaseRecord);
		}else{
			ret = fireCaseRecordDao.update(fireCaseRecord);
		}
		return ret;
	}

	/*
	*删除
	*/
	@Override
	public int delFireCaseRecord(MsgBean mbean,FireCaseRecord fireCaseRecord){
		return fireCaseRecordDao.del(fireCaseRecord);
	}

}
